library(foreign)
library(tidyverse)
library(readstata13)

rm(list=ls())

data1<-read.csv("Code/Study2/Study2_Clean.csv")
data2<-data.frame(data1)
colnames(data2)
summary(data2)

#Step 1 - Coding Conjoint Up####

## enter id here, you could also use 1:nrow(d) instead of rownames
resp_id <- rownames(data2)
resp_id
data3 <- cbind(resp_id, data2)
colnames(data3)
data3$resp_id

##Code Difference in Intensity Score 

data3<-data3 %>% 
  mutate("1_diff1" = X1_int_1-X1_int_2) %>% 
  mutate("1_diff2"= X1_int_2-X1_int_1) %>% 
  mutate("2_diff1" = X2_int_1-X2_int_2) %>% 
  mutate("2_diff2"= X2_int_2-X2_int_1) %>% 
  mutate("3_diff1" = X3_int_1-X3_int_2) %>% 
  mutate("3_diff2"= X3_int_2-X3_int_1) %>% 
  mutate("4_diff1" = X4_int_1-X4_int_2) %>% 
  mutate("4_diff2"= X4_int_2-X4_int_1) %>% 
  mutate("5_diff1" = X5_int_1-X5_int_2) %>% 
  mutate("5_diff2"= X5_int_2-X5_int_1) 

##Rename DV
colnames(data3)

data4<-data3 %>% 
  rename(
    "1_pref" = "X1_pref",
    "2_pref" = "X2_pref",
    "3_pref" = "X3_pref",
    "4_pref" = "X4_pref",
    "5_pref" = "X5_pref",
    "1_intent1" = "X1_int_1",
    "2_intent1" = "X2_int_1",
    "3_intent1" = "X3_int_1",
    "4_intent1" = "X4_int_1",
    "5_intent1" = "X5_int_1",
    "1_intent2" = "X1_int_2",
    "2_intent2" = "X2_int_2",
    "3_intent2" = "X3_int_2",
    "4_intent2" = "X4_int_2",
    "5_intent2" = "X5_int_2")
colnames(data4)

d_master_conjoint<-data4

#Formatting Dataset so that each polling location has a row
d_loc <- d_master_conjoint %>% select(resp_id, starts_with("choice"))
d_loc <- d_loc %>% 
  gather(variable, value, -resp_id) %>% 
  mutate( 
    choiceNum=gsub("[A-Za-z]|_.+","",variable),
    locNum=gsub(".+(.$)","\\1",variable),
    attribute=gsub(".+_|.$","",variable)
  ) %>%select(-variable) %>% 
  spread(attribute, value)

d_loc
d_loc %>% filter(resp_id==1)

#Reshape - Preference Outcome 

d_pref <- d_master_conjoint %>% select(resp_id, ends_with("pref"))
d_pref <- d_pref %>%  
  gather(variable, preference, -resp_id) %>% 
  mutate( 
    choiceNum = gsub("_pref","",variable),
    preference=as.numeric(gsub("","",preference))
  ) %>% 
  select(-variable)
d_pref
d_pref %>% filter(resp_id==1)

#Reshape - Intensity difference. This dataset records the preference difference 
d_diff1 <- d_master_conjoint %>% select(resp_id, ends_with("diff1"))
d_diff1 <- d_diff1 %>%  
  gather(variable, diff, -resp_id) %>% 
  mutate( 
    choiceNum = gsub("_diff1","",variable),
    diff=as.numeric(gsub("","",diff))
  ) %>% 
  select(-variable)

d_diff2 <- d_master_conjoint %>% select(resp_id, ends_with("diff2"))
d_diff2<- d_diff2 %>%  
  gather(variable, diff, -resp_id) %>% 
  mutate( 
    choiceNum = gsub("_diff2","",variable),
    diff=as.numeric(gsub("","",diff))
  ) %>% 
  select(-variable)

d_diff1<-d_diff1 %>% add_column(locNum=as.character(1))
d_diff2<-d_diff2 %>% add_column(locNum=as.character(2))
d_diff1 %>% filter(resp_id==779) 
d_diff2 %>% filter(resp_id==779) 

d_diff_merged<-bind_rows(d_diff1, d_diff2) 
d_diff_merged %>% filter(resp_id==1) 

##Stack Dataset 
#Step 1 - Join the dataset with conjoint attributes with the dataset that records profile preference 
d_stack <- left_join(d_loc, d_pref)

#Step 2 - Join the datasets that measure profile preference intensity 
d_stack2<-left_join(d_stack, d_diff_merged)

d_stack2 %>% filter(resp_id==2)

d_stack3 <- d_stack2 %>% 
  mutate (
    Y_choice=as.numeric(locNum==preference),
  )
d_stack3

d_stack3 %>% filter(resp_id==50)

#Data Check 
nrow(d_stack3) 
summary(d_stack3)

##Merge Back in Individual Level Data

dstack_merge<- left_join(d_stack3, data3, by="resp_id")
summary(dstack_merge)
nrow(dstack_merge)

##Write 

save.dta13(dstack_merge, "Code/Study2/merged_conjoint_study2.dta")




